1   /*
2    * Copyright (c) 2004-2005, University Health Network.  All rights reserved. Distributed under the BSD 
3    * license (see http://opensource.org/licenses/bsd-license.php).
4    *  
5    * HibernateQueryResultStoreTest.java
6    *
7    * Created on 16-Dec-2004 at 2:52:55 PM
8    */
9   package ca.uhn.cache.internal.impl;
10  
11  import java.util.ArrayList;
12  import java.util.Date;
13  
14  import net.sf.hibernate.Hibernate;
15  import net.sf.hibernate.Session;
16  
17  import org.jmock.Mock;
18  import org.jmock.cglib.MockObjectTestCase;
19  import org.jmock.core.Invocation;
20  import org.jmock.core.Stub;
21  import org.springframework.orm.hibernate.HibernateCallback;
22  import org.springframework.orm.hibernate.HibernateTemplate;
23  
24  import ca.uhn.cache.IDimension;
25  import ca.uhn.cache.IParamSpace;
26  import ca.uhn.cache.VolatilityEnum;
27  import ca.uhn.cache.impl.DataItem;
28  import ca.uhn.cache.impl.DateParam;
29  import ca.uhn.cache.impl.DateRangeParam;
30  import ca.uhn.cache.impl.Dimension;
31  import ca.uhn.cache.impl.Query;
32  import ca.uhn.cache.impl.QueryResult;
33  import ca.uhn.cache.impl.StringParam;
34  import ca.uhn.cache.impl.StringSetParam;
35  import ca.uhn.cache.internal.exception.QueryResultStoreException;
36  import ca.uhn.cache.internal.hibernate.impl.DateParamField;
37  import ca.uhn.cache.internal.hibernate.impl.Record;
38  import ca.uhn.cache.internal.hibernate.impl.StringParamField;
39  import ca.uhn.cache.internal.util.UTC;
40  
41  
42  /***
43   * TODO complete javadoc for 
44   * 
45   * @author <a href="mailto:alexei.guevara@uhn.on.ca">Alexei Guevara</a>
46   * @version $Revision: 1.1 $ updated on $Date: 2005/01/24 22:52:11 $ by $Author: bryan_tripp $
47   */
48  public class HibernateQueryResultStoreTest extends MockObjectTestCase {
49      
50      private IDimension myStringParamDim;
51      private IDimension myDateParamDim;
52      
53      private HibernateQueryResultStore myHibernateQueryResultStore;
54      
55      private Mock myHibernateTemplateMock;
56      private HibernateTemplate myHibernateTemplate;
57      
58      private Mock mySessionMock;
59      private Session mySession;
60      
61      private Mock myParamSpaceMock;
62      private IParamSpace myParamSpace;
63      
64  
65      /***
66       * jmock stub for the method HibernateTemplate.execute( HibernateCallback )
67       */
68      private static class HibernateTemplateExecuteStub implements Stub {
69          private final Session mySession;
70          /***
71           * @param theSession The session to be used when executing the <code>HibernateCallback</code>.
72           */
73          public HibernateTemplateExecuteStub( Session theSession ) {
74              mySession = theSession;
75          }
76          /***
77           * {@inheritDoc}
78           */
79          public Object invoke( Invocation theInvocation ) throws Throwable {
80              HibernateCallback hibernateCallback = (HibernateCallback) theInvocation.parameterValues.get(0);
81              return hibernateCallback.doInHibernate( mySession );
82          }
83          /***
84           * {@inheritDoc}
85           */
86          public StringBuffer describeTo( StringBuffer theBuffer ) {
87              return new StringBuffer( "stub for the method HibernateTemplate.execute( HibernateCallback )" );
88          }
89      }
90  
91      /*
92       * @see TestCase#setUp()
93       */
94      protected void setUp() throws Exception {
95          super.setUp();
96          
97          myStringParamDim = new Dimension( "stringDimension", new Class[] { StringParam.class, StringSetParam.class } );
98          myDateParamDim = new Dimension( "dateDimension", new Class[] { DateParam.class, DateRangeParam.class } );
99  
100         mySessionMock = mock( Session.class, "mySessionMock" );
101         mySession = (Session) mySessionMock.proxy();
102         
103         myHibernateTemplateMock = mock( HibernateTemplate.class, "myHibernateTemplateMock" );
104         myHibernateTemplate = (HibernateTemplate) myHibernateTemplateMock.proxy();
105 
106         myHibernateTemplateMock.expects( once() )
107             .method("execute")
108             .with( isA( HibernateCallback.class ))
109             .will( new HibernateTemplateExecuteStub( mySession ) );
110         
111         myParamSpaceMock = mock( IParamSpace.class, "myParamSpaceMock" );
112         myParamSpace = (IParamSpace) myParamSpaceMock.proxy();
113         
114         myParamSpaceMock.expects( once() )
115             .method("getDimensions")
116             .withNoArguments()
117             .will( returnValue( new IDimension[] { myStringParamDim, myDateParamDim } ) );
118 
119         myHibernateQueryResultStore = new HibernateQueryResultStore();
120         myHibernateQueryResultStore.setHibernateTemplate( myHibernateTemplate );
121         myHibernateQueryResultStore.setParamSpace( myParamSpace );
122         
123     }
124 
125     /*
126      * @see TestCase#tearDown()
127      */
128     protected void tearDown() throws Exception {
129         super.tearDown();
130     }
131 
132     /***
133      * - one record that maps to a query with one StringParam.
134      * @throws QueryResultStoreException ...
135      */
136     public void testInsert1() throws QueryResultStoreException {
137         Query diProj = new Query();
138         diProj.addParameter( new StringParam( myStringParamDim, "a" ) );
139 
140         Date date = new Date();        
141         DataItem dataItem = new DataItem( "id", "", diProj, VolatilityEnum.STABLE, date );
142         
143         QueryResult qr = new QueryResult();
144         qr.add( dataItem );
145         
146         mySessionMock.expects( once() )
147             .method( "find" )
148                 .with( isA( String.class ), eq( "id" ), eq( Hibernate.STRING ) )
149                 .will( returnValue( new ArrayList() ) ).id("0");
150         
151         mySessionMock.expects( once() ).method( "save" ).with( isA( Record.class ) ).id("1");
152         mySessionMock.expects( once() ).method( "save" ).with( isA( StringParamField.class ) ).after("1").id("2");
153         
154         myHibernateQueryResultStore.insert( qr );
155     }
156     
157     /***
158      * - one record that maps to a query with one DateRangeParam.
159      * @throws QueryResultStoreException ...
160      */
161     public void testInsert2() throws QueryResultStoreException {
162         Date date = new Date();
163 
164         Query diProj = new Query();
165         diProj.addParameter( new DateParam( myDateParamDim, UTC.currentTime() ) );
166         
167         DataItem dataItem = new DataItem( "id", "", diProj, VolatilityEnum.STABLE, date );
168         
169         QueryResult qr = new QueryResult();
170         qr.add( dataItem );
171         
172         mySessionMock.expects( once() )
173             .method( "find" )
174                 .with( isA( String.class ), eq( "id" ), eq( Hibernate.STRING ) )
175                 .will( returnValue( new ArrayList() ) ).id("0");
176         
177         mySessionMock.expects( once() ).method( "save" ).with( isA( Record.class ) ).id("1");
178         mySessionMock.expects( once() ).method( "save" ).with( isA( DateParamField.class ) ).after("1").id("2");
179         
180         myHibernateQueryResultStore.insert( qr );
181     }
182     
183 }